package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.mapper.DtsGrouponRulesMapper;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGrouponRules;
import com.qf.service.DtsGrouponRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class DtsGrouponRulesServiceImpl implements DtsGrouponRulesService {

    @Autowired
    DtsGrouponRulesMapper grouponRulesMapper;
    @Autowired
    DtsGoodsMapper goodsMapper;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param goodsId
     * @return
     */
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, Integer goodsId) {
        IPage<DtsGrouponRules> iPage = new Page<>(page,limit);
        QueryWrapper<DtsGrouponRules> wrapper = new QueryWrapper<>();
        //判断goodsId是否为空
        if (goodsId != null){
            wrapper.eq("goods_id",goodsId);
        }
        //判断升序降序
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        grouponRulesMapper.selectPage(iPage,wrapper);

        //封装返回值对象
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());

        return data;
    }

    /**
     * 添加
     * @param discount
     * @param discountMember
     * @param expireTime
     * @param goodsId
     */
    @Override
    public void insert(String discount, Integer discountMember, String expireTime, Long goodsId,String goodsName) {
        DtsGrouponRules grouponRules = new DtsGrouponRules();

        grouponRules.setDiscount(new BigDecimal(discount));
        grouponRules.setDiscountMember(discountMember);
        grouponRules.setExpireTime(expireTime);
        grouponRules.setGoodsId(goodsId);

        //条件查询
        LambdaQueryWrapper<DtsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGoods::getId,goodsId);
        DtsGoods goods = goodsMapper.selectOne(wrapper);
        if (goods == null){
            throw new RuntimeException("没有此商品");
        }

        //通过商品id获取商品名称
        grouponRules.setGoodsName(goods.getName());
        grouponRules.setAddTime(new Date());
        grouponRules.setUpdateTime(new Date());
        grouponRules.setDeleted(false);

        grouponRulesMapper.insert(grouponRules);
    }

    /**
     * 删除
     * @param grouponRules
     */
    @Override
    public void delete(DtsGrouponRules grouponRules) {
        grouponRulesMapper.deleteById(grouponRules.getId());
    }

    /**
     * 修改
     * @param grouponRules
     */
    @Override
    public void update(DtsGrouponRules grouponRules) {
        grouponRulesMapper.updateById(grouponRules);
    }
}
